<?php
	class School extends MY_controller{

		function __construct(){
			parent::__construct();
		}

	function index($status = '9', $school_name = 0, $agent_id = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$school_name = $post_data['school_name'] ? urldecode($post_data['school_name']) : 0;
			$status = isset($post_data['status']) ? $post_data['status'] : '9';
			$agent_id = isset($post_data['agent_id']) ? $post_data['agent_id'] : 0;
		}
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$conditions = array('status' => $status, 'school_name' => $school_name, "agent_id" => $agent_id);
		$this->load->model("Schools");
		$result = $this->Schools->get_school_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/school/index/' . $status . '/' . $school_name . '/' . $agent_id;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 6;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->load->model("Agents");
		$agent_list = $this->Agents->get_agent_list();
		$this->smarty->assign("action", '网校列表');
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("current_url", $current_url);
		$this->smarty->assign("school_list", $result['result']);
		$this->smarty->assign("agent_list", $agent_list);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("school/list.html");
	}

	function edit($id = 0, $type = 1, $limit = 0){
		$post_data = $this->input->post();
		if($post_data){
			if(isset($post_data['run_status'])){
				$data['status'] = $post_data['status'];
				$data['run_status'] = $post_data['run_status'];
				$data['feedback'] = $post_data['feedback'];
				$data['agent_id'] = $post_data['agent_id'];
				$data['item_num'] = $post_data['item_num'];
				$data['start_date'] = $post_data['start_date'];
				$data['end_date'] = $post_data['end_date'];
				if(isset($post_data['file'])) unset($post_data['file']);
				
				$this->db->where('id', $post_data['id']);
				$this->db->update('o2o_school', $post_data);
			}elseif(isset($post_data['privilege'])){
				unset($post_data['privilege']);
				$school_privileges = get_field_by_where("school_privileges", "*", "school_id='{$post_data['school_id']}'");
				if(empty($school_privileges)){
					$this->db->insert("school_privileges", $post_data);
				}else{
					$data['upload_video'] = empty($post_data['upload_video']) ? 0 : $post_data['upload_video'];
					$data['video_course'] = empty($post_data['video_course']) ? 0 : $post_data['video_course'];
					$data['live'] = empty($post_data['live']) ? 0 : $post_data['live'];
					$data['offline_course'] = empty($post_data['offline_course']) ? 0 : $post_data['offline_course'];
					$data['add_topic'] = empty($post_data['add_topic']) ? 0 : $post_data['add_topic'];
					$data['add_topic_volumes'] = empty($post_data['add_topic_volumes']) ? 0 : $post_data['add_topic_volumes'];
					$data['add_train'] = empty($post_data['add_train']) ? 0 : $post_data['add_train'];
					$data['tixian'] = empty($post_data['tixian']) ? 0 : $post_data['tixian'];
					$this->db->where("school_id", $post_data['school_id']);
					$this->db->update("school_privileges", $data);	
				}
				
				/*$edit_array = $del_array = $add_array = array();
				$this->load->model("Schools");
				$bd_category = $this->Schools->get_school_system_topics($post_data['id']);
				$list_key = array_column($bd_category, "category_id");
				$fx_category = $this->Schools->get_school_system_video_course($post_data['id']);
				$fx_list_key = array_column($fx_category, "category_id");
				if(isset($post_data['is_bind'])){
					$time = time();
					$code_data = array();
					foreach ($post_data['is_bind'] as $key => $value) {
						if(in_array($key, $list_key)){
							$this->db->where("category_id", $key);
							$this->db->where("school_id", $post_data['id']);
							$this->db->set("total_code_num", "total_code_num + {$post_data['total_code_num'][$key]}", false);
							$this->db->set("surplus_code_num", "surplus_code_num + {$post_data['total_code_num'][$key]}", false);
							$this->db->update("o2o_school_system_topics", array("expire_tiem" => strtotime($post_data['expire_tiem'][$key])));
						}elseif(!in_array($key, $list_key) && $post_data['expire_tiem'][$key] != '' && $post_data['total_code_num'][$key] > 0){
							$this->db->insert("o2o_school_system_topics", array("category_id" => $key, "school_id" => $post_data['id'], "expire_tiem" => strtotime($post_data['expire_tiem'][$key]), "total_code_num" => $post_data['total_code_num'][$key], "surplus_code_num" => $post_data['total_code_num'][$key], "add_time" => $time));
						}
						for ($i=0; $i < $post_data['total_code_num'][$key] ; $i++) { 
							$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
							$code = $yCode[intval(date('Y')) - 2011 - $i] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
							$code_data[] = array("school_id" => $post_data['id'], "category_id" => $key, "add_time" => $time, "expire_tiem" => strtotime($post_data['expire_tiem'][$key]), "code" => "CT" . $code);
						}
					}
					$this->db->insert_batch("o2o_school_system_topic_codes", $code_data);
					foreach ($list_key as $key => $value) {
						if(!in_array($value, array_keys($post_data['is_bind']))){
							$this->db->where('category_id', $value);
							$this->db->where("school_id", $post_data['id']);
							$result = $this->db->delete('o2o_school_system_topics');

							$this->db->where('category_id', $value);
							$this->db->where("school_id", $post_data['id']);
							$result = $this->db->delete('o2o_school_system_topic_codes');
						}
					}
				}else{
					foreach ($list_key as $key => $value) {
						$this->db->where('category_id', $value);
						$this->db->where("school_id", $post_data['id']);
						$result = $this->db->delete('o2o_school_system_topics');

						$this->db->where('category_id', $value);
						$this->db->where("school_id", $post_data['id']);
						$result = $this->db->delete('o2o_school_system_topic_codes');
					}
				}

				if(isset($post_data['is_course_bind'])){
					$time = time();
					$code_data = array();
					foreach ($post_data['is_course_bind'] as $key => $value) {
						if(in_array($key, $fx_list_key)){
							$this->db->where("category_id", $key);
							$this->db->where("school_id", $post_data['id']);
							$this->db->set("total_code_num", "total_code_num + {$post_data['total_code_num'][$key]}", false);
							$this->db->set("surplus_code_num", "surplus_code_num + {$post_data['total_code_num'][$key]}", false);
							$this->db->update("o2o_school_system_video_course", array("expire_tiem" => strtotime($post_data['expire_tiem'][$key])));
						}elseif(!in_array($key, $fx_list_key) && $post_data['expire_tiem'][$key] != '' && $post_data['total_code_num'][$key] > 0){
							
							$this->db->insert("o2o_school_system_video_course", array("category_id" => $key, "school_id" => $post_data['id'], "expire_tiem" => strtotime($post_data['expire_tiem'][$key]), "total_code_num" => $post_data['total_code_num'][$key], "surplus_code_num" => $post_data['total_code_num'][$key], "add_time" => $time));
						}
						for ($i=0; $i < $post_data['total_code_num'][$key] ; $i++) { 
							$yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
							$code = $yCode[intval(date('Y')) - 2011 - $i] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
							$code_data[] = array("school_id" => $post_data['id'], "category_id" => $key, "add_time" => $time, "expire_tiem" => strtotime($post_data['expire_tiem'][$key]), "code" => "DV" . $code);
						}
					}
					$this->db->insert_batch("o2o_school_system_video_course_codes", $code_data);
					foreach ($list_key as $key => $value) {
						if(!in_array($value, array_keys($post_data['is_course_bind']))){
							$this->db->where('category_id', $value);
							$this->db->where("school_id", $post_data['id']);
							$result = $this->db->delete('o2o_school_system_video_course');

							$this->db->where('category_id', $value);
							$this->db->where("school_id", $post_data['id']);
							$result = $this->db->delete('o2o_school_system_video_course_codes');
						}
					}
				}else{
					foreach ($list_key as $key => $value) {
						$this->db->where('category_id', $value);
						$this->db->where("school_id", $post_data['id']);
						$result = $this->db->delete('o2o_school_system_video_course');

						$this->db->where('category_id', $value);
						$this->db->where("school_id", $post_data['id']);
						$result = $this->db->delete('o2o_school_system_video_course_codes');
					}
				}	*/
				
			}
			show_message($_SERVER['HTTP_REFERER']);
		}else{
			if(intval($id) > 0){
				$school_info = get_field_by_where("o2o_school", "*", "id='{$id}'");
				if(!$school_info){
					show_message("/o2o_school/index", "数据不存在");exit;
				}
				$this->load->model("Schools");
				$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$conditions = array('school_id' => $id);
				$this->load->model("Areas");
				$province_list = $this->Areas->get_province_data();
				if($school_info['province_code'] > 0){
					$city_list = $this->Areas->get_province_data($school_info['province_code']);
					$this->smarty->assign("city_list", $city_list);
				}

				if($school_info['city_code'] > 0){
					$area_list = $this->Areas->get_province_data($school_info['city_code']);
					$this->smarty->assign("area_list", $area_list);
				}
				$this->smarty->assign("province_list", $province_list);
				if($type == 2){
					$result = $this->Schools->get_school_teachers($limit, $this->offset, $conditions);
					$this->load->library('pagination');
					$page_config['base_url'] = '/school/edit/' . $id . '/' . '2';
					$page_config['total_rows'] = $result['count'];
					$page_config['per_page'] = $this->offset;
					$page_config['uri_segment'] = 5;
					$this->pagination->initialize($page_config);
					$pagination = $this->pagination->create_links();
					$this->smarty->assign("result", $result['result']);
					$this->smarty->assign("pagination", $pagination);
				}elseif($type == 3){
					$result = $this->Schools->get_school_courses($limit, $this->offset, $conditions);
					$this->load->library('pagination');
					$page_config['base_url'] = '/school/edit/' . $id . '/3';
					$page_config['total_rows'] = $result['count'];
					$page_config['per_page'] = $this->offset;
					$page_config['uri_segment'] = 5;
					$this->pagination->initialize($page_config);
					$pagination = $this->pagination->create_links();
					$this->smarty->assign("result", $result['result']);
					$this->smarty->assign("pagination", $pagination);
				}elseif($type == 4){
					$this->load->model("Categorys");
					$category_list = $this->Categorys->get_category_data($id);
					if($category_list){
						$bd_category = $this->Schools->get_school_system_topics($id);
						$list_key = array_column($bd_category, "category_id");
						foreach ($category_list as $key => $value) {
							$category_list[$key]['is_bind'] = 0;
							$category_list[$key]['expire_tiem'] = '';
							foreach ($bd_category as $k => $v) {
								if($v['category_id'] == $value['id']){
									$category_list[$key]['is_bind'] = 1;
									$category_list[$key]['expire_tiem'] = $v['expire_tiem'];
									$category_list[$key]['total_code_num'] = $v['total_code_num'];
									$category_list[$key]['surplus_code_num'] = $v['surplus_code_num'];
								}
							}
						}
					}
					$this->smarty->assign("result", $category_list);
				}elseif($type == 5){
					/*
					$category_list = $this->Schools->get_course_cate();
					if($category_list){
						$bd_category = $this->Schools->get_school_system_video_course($id);
						$list_key = array_column($bd_category, "category_id");
						foreach ($category_list as $key => $value) {
							$category_list[$key]['is_course_bind'] = 0;
							$category_list[$key]['expire_tiem'] = '';
							foreach ($bd_category as $k => $v) {
								if($v['category_id'] == $value['id']){
									$category_list[$key]['is_course_bind'] = 1;
									$category_list[$key]['expire_tiem'] = $v['expire_tiem'];
									$category_list[$key]['total_code_num'] = $v['total_code_num'];
									$category_list[$key]['surplus_code_num'] = $v['surplus_code_num'];
								}
							}
						}
					}
					$this->smarty->assign("result", $category_list);*/
					$this->load->model("Courses");
					$page_config['uri_segment'] = 5;
					$offset = 15;
					$data = array( 'status' => 10, 'type' => 1);
					$result = $this->Courses->get_list_ofpage($limit, $offset,$data);
					$this->load->library('pagination');
					$page_config['base_url'] = '/school/edit/'.$id.'/'.$type;
					$page_config['total_rows'] = $result['count'];
					$page_config['per_page'] = $offset;
					$this->pagination->initialize($page_config);
					$pagination = $this->pagination->create_links();
					$this->smarty->assign("pagination", $pagination);
					$this->smarty->assign("result", $result['result']);
				}elseif($type == 6){
					$data = array( 'status' => 10, 'type' => 1);
					$school_privileges = get_field_by_where("school_privileges", "*", "school_id='{$id}'");
					if(empty($school_privileges)){
						$school_privileges['upload_video'] = $school_privileges['live'] = $school_privileges['video_course'] = $school_privileges['offline_course'] = $school_privileges['add_topic'] = $school_privileges['add_topic_volumes'] = $school_privileges['add_train'] = 0;
					}
					$this->smarty->assign("school_privileges", $school_privileges);
				}
				$this->load->model("Agents");
				$agent_list = $this->Agents->get_agent_list();
				$this->smarty->assign("type", $type);
				$this->smarty->assign("school_info", $school_info);
				$this->smarty->assign("agent_list", $agent_list);
				$this->smarty->assign("action", '网校详情');
				$this->smarty->assign("post_url", '/school/edit');
				$this->smarty->display("school/save.html");
			}else{
				show_message("/news_category", "非法操作");
			}
		}
	}

	function order_record( $school_id = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$school_id = isset($post_data['school_id']) ? $post_data['school_id'] : '0';
		}
		$this->load->model("Order_course_auths");
		$conditions['school_id'] = $school_id;
		$record_list = $this->Order_course_auths->get_order_record_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/school/order_record/' . $school_id;
		$page_config['total_rows'] = $record_list['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("action", '网校消费记录');
		$this->smarty->assign("record_list", $record_list['result']);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("type", 12);
		$school_info['id'] = $school_id;
		$this->smarty->assign("school_info", $school_info);
		$this->smarty->display("school/order_record.html");
	}
}